package com.bestcem.xm.qdes.grpc.client;

import com.bestcem.xm.bi.grpc.v1.services.*;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
public class BiGrpcClient {
    @GrpcClient(GrpcClientNames.DS_BI)
    private BiServiceGrpc.BiServiceBlockingStub biServiceBlockingStub;
    @GrpcClient(GrpcClientNames.DS_BI)
    private BaseBiServiceGrpc.BaseBiServiceBlockingStub baseBiServiceBlockingStub;

    public int countData(String worktableId, List<Filter> filters, NewColumn countColumn, Pivot pivot) {
        SearchRequest.Builder builder = SearchRequest.newBuilder();
        builder.setWorktableId(worktableId);
        builder.setSkip(0).setLimit(0);
        builder.addAllFilters(filters);
        builder.addAllNewColumns(Collections.singletonList(countColumn));
        builder.addAllPivots(Collections.singletonList(pivot));
        try {
            SearchResponse response = biServiceBlockingStub.getPivotData(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] 调用BI getPivotData返回 error worktableId:{} filters:{} countColumn:{} pivot:{}",
                        worktableId, filters, countColumn, pivot);
                return 0;
            }
            return getCount(response);
        } catch (Exception e) {
            log.error("[qdes] 调用BI getPivotData接口失败 worktableId:{} filters:{} countColumn:{} pivot:{}",
                    worktableId, filters, countColumn, pivot, e);
            return 0;
        }
    }

    public List<Column> listColumn(String worktableId) {
        ListColumnRequest.Builder builder = ListColumnRequest.newBuilder();
        Column column = Column.newBuilder().setWorktableId(worktableId).build();
        builder.setColumn(column);
        try {
            ColumnSearchResponse response = baseBiServiceBlockingStub.listColumn(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] 调用BI listColumn返回 error worktableId:{}", worktableId);
                return null;
            }
            return response.getColumnListList();
        } catch (Exception e) {
            log.error("[qdes] 调用BI listColumn接口失败 worktableId:{}", worktableId);
            return null;
        }
    }

    private int getCount(SearchResponse searchResponse) {
        AtomicInteger count = new AtomicInteger();
        List<RowValue> bodyValuesList = searchResponse.getBodyValuesList();
        if (!CollectionUtils.isEmpty(bodyValuesList)) {
            bodyValuesList.stream().findFirst().ifPresent(rowValue -> {
                if (!CollectionUtils.isEmpty(rowValue.getDataList())) {
                    count.set(new BigDecimal(rowValue.getData(0)).intValue());
                }
            });

        }
        return count.get();
    }
}
